package com.example.helloworld.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;

import com.example.helloworld.R;

public class TitleBitmapButton extends Button {

	public static final int BITMAP_ALIGN_CENTER =	0;
	public static final int BITMAP_ALIGN_LEFT =		1;
	public static final int BITMAP_ALIGN_RIGHT =	2;
	
	private int defaultColor =			0xFFFFFFFF;
	private float defaultSize =			18F;
	private float defaultScaleX =		1.0F;
	private int bitmapAlign =			BITMAP_ALIGN_CENTER;
	private int bitmapPadding =			10;
	private Typeface defaultTypeFace =	Typeface.DEFAULT_BOLD;  
	private String titleText =			"";
	
	private int iconStatus =			0; 
	
	private Context context;
	private Paint paint;
	private Bitmap iconNormalBitmap;
	private Bitmap iconClickedBitmap;
	
	private boolean paintChanged =	false;
	
	public TitleBitmapButton(Context context) {
		super(context);
		
		this.context =	context;
		init();
	}

	public TitleBitmapButton(Context context, AttributeSet atts) {
		super(context, atts);
		
		this.context =	context;
		init();
	}
	
	public void init() {
		setBackgroundResource(R.drawable.title_bitmap_button_normal);
		
		paint =	new Paint();
		paint.setColor(defaultColor);
		paint.setAntiAlias(true);
		paint.setTextScaleX(defaultScaleX);
		paint.setTextSize(defaultSize);
		paint.setTypeface(defaultTypeFace);
	}
	
	public void setIconBitmap(Bitmap iconNormal, Bitmap iconClicked) {
		iconNormalBitmap =	iconNormal;
		iconClickedBitmap =	iconClicked;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		
		int action =	event.getAction();
		
		switch (action) {
		case MotionEvent.ACTION_OUTSIDE:
		case MotionEvent.ACTION_CANCEL:
		case MotionEvent.ACTION_UP:
			setBackgroundResource(R.drawable.title_bitmap_button_normal);
			
			iconStatus =	0;
			
			break;
		case MotionEvent.ACTION_DOWN:
			setBackgroundResource(R.drawable.title_bitmap_button_clicked);
			
			iconStatus =	1;
			
			break;
		}
		
		invalidate();
		
		return true;
	}
	
	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		int width =		getWidth();
		int height =	getHeight();
		
		if (paintChanged) {
			paint.setColor(defaultColor);
			paint.setTextScaleX(defaultScaleX);
			paint.setTextSize(defaultSize);
			paint.setTypeface(defaultTypeFace);
		}
		
		Bitmap iconBitmap =	iconNormalBitmap;
		if (iconStatus == 1) {
			iconBitmap =	iconClickedBitmap;
		}
		
		if (iconBitmap != null) {
			int iconWidth =		iconBitmap.getWidth();
			int iconHeight =	iconBitmap.getHeight();
			
			int bitmapX =		0;
			
			if (bitmapAlign == BITMAP_ALIGN_CENTER) {
				bitmapX =	(width - iconWidth) / 2;			
			} else
			if (bitmapAlign == BITMAP_ALIGN_LEFT) {
				bitmapX =	bitmapPadding;
			} else
			if (bitmapAlign == BITMAP_ALIGN_RIGHT) {
				bitmapX =	width - bitmapPadding;
			}
			
			canvas.drawBitmap(iconBitmap, bitmapX, (height - iconHeight) / 2, paint);
		}
		
		Rect bounds =	new Rect();
		paint.getTextBounds(titleText, 0, titleText.length(), bounds);
		
		float textWidth =	((float)width - bounds.width()) / 2.0F;
		float textHeight =	(((float)(height - 4) + bounds.height()) / 2.0F) - 1.0F;
		
		canvas.drawText(titleText, textWidth, textHeight, paint);
	}
	
	public int getDefaultColor() {
		return defaultColor;
	}

	public void setDefaultColor(int defaultColor) {
		this.defaultColor = defaultColor;
		paintChanged =	true;
	}

	public float getDefaultSize() {
		return defaultSize;
	}

	public void setDefaultSize(float defaultSize) {
		this.defaultSize = defaultSize;
		paintChanged =	true;
	}

	public float getDefaultScaleX() {
		return defaultScaleX;
	}

	public void setDefaultScaleX(float defaultScaleX) {
		this.defaultScaleX = defaultScaleX;
		paintChanged =	true;
	}

	public Typeface getDefaultTypeFace() {
		return defaultTypeFace;
	}

	public void setDefaultTypeFace(Typeface defaultTypeFace) {
		this.defaultTypeFace = defaultTypeFace;
		paintChanged =	true;
	}

	public String getTitleText() {
		return titleText;
	}

	public void setTitleText(String titleText) {
		this.titleText = titleText;
		paintChanged =	true;
	}

	public int getBitmapAlign() {
		return bitmapAlign;
	}

	public void setBitmapAlign(int bitmapAlign) {
		this.bitmapAlign = bitmapAlign;
		paintChanged =	true;
	}

	public int getBitmapPadding() {
		return bitmapPadding;
	}

	public void setBitmapPadding(int bitmapPadding) {
		this.bitmapPadding = bitmapPadding;
		paintChanged =	true;
	}
}
